﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolUtil;
using zyconfig;

namespace zyui
{
    public class GetSjhm
    {
        public static int SearchSjhm(string strHanzi, out string strSjhm, out string strError)
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "字符='" + strHanzi + "'";
                strSjhm = ""; strError = "";
                if (!_open.Exist("四角号码表"))
                {
                    strError = "编码不存在";
                    return 0;
                }
                else
                {
                    strSjhm = _open.GetField("四角号码表", "编码").ToString().Trim();
                    return 1;
                }
            }
        }
        public static int HanziTextToSjhm(bool bLocal, string strText, out List<string> sjhms, out string strError)
        {
            strError = "";
            sjhms = new List<string>();
            for (int i = 0; i < strText.Length; i++)
            {
                char ch = strText[i];
                if (StringUtil.IsHanzi(ch) == false)
                    continue;
                // 是否特殊符号
                if (StringUtil.SpecialChars.IndexOf(ch) != -1)
                    continue;

                // 汉字
                string strHanzi = "";
                strHanzi += ch;
                string strResultSjhm = "";
                int nRet = 0;
                if (bLocal == true)
                {
                    nRet = SearchSjhm(strHanzi, out strResultSjhm, out strError);
                }
                else
                {
                    throw new Exception("暂不支持从拼音库中获取四角号码");
                    /*
                    nRet = GetOnePinyin(strHanzi,
                         out strResultPinyin,
                         out strError);
                     * */
                }
                if (nRet == -1)
                    return -1;
                if (nRet == 0)
                {	// canceled
                    return 0;
                }
                Debug.Assert(strResultSjhm != "", "");
                strResultSjhm = strResultSjhm.Trim();
                sjhms.Add(strResultSjhm);
            }
            return 1;   // 正常结束
        }
        public static int PrepareSjhmAuthorString(string strAuthor,out string strResult,out string strError)
        {
            strResult = "";
            strError = "";
            for (int i = 0; i < strAuthor.Length; i++)
            {
                char ch = strAuthor[i];
                if (StringUtil.IsHanzi(ch) == false)
                    continue;

                //是否特殊符号
                if (StringUtil.SpecialChars.IndexOf(ch) != -1)
                {
                    continue;
                }
                // 汉字
                strResult += ch;
            }

            return 0;
        }
        public static int GetSjhmAuthorNumber(string strAuthor,out string strAuthorNumber,out string strError)
        {
            strError = "";
            strAuthorNumber = "";
            string strResult = "";
            int nRet = PrepareSjhmAuthorString(strAuthor,out strResult,out strError);
            if (nRet == -1)
                return -1;
            if (nRet == -1)
                return -1;
            if (String.IsNullOrEmpty(strResult) == true)
            {
                strError = "著者字符串 '" + strAuthor + "' 里面没有包含有效的汉字字符";
                return -1;
            }
            List<string> sjhms = null;
            // 把字符串中的汉字转换为四角号码
            // parameters:
            //      bLocal  是否从本地获取四角号码
            // return:
            //      -1  出错
            //      0   用户希望中断
            //      1   正常
            nRet = HanziTextToSjhm(true,strResult,out sjhms,out strError);
            if (nRet != 1)
                return nRet;

            if (strResult.Length != sjhms.Count)
            {
                strError = "著者字符串 '" + strResult + "' 里面的字符数(" + strResult.Length.ToString() + ")和取四角号码后的结果事项个数 " + sjhms.Count.ToString() + " 不符";
                return -1;
            }

            // 1，著者名称为一字者，取该字的四角号码。如：肖=9022
            if (strResult.Length == 1)
            {
                strAuthorNumber = sjhms[0].Substring(0, 4);
                return 1;
            }
            // 2，著者名称为二字者，分别取两个字的左上角和右上角。如：刘翔=0287
            if (strResult.Length == 2)
            {
                strAuthorNumber = sjhms[0].Substring(0, 2) + sjhms[1].Substring(0, 2);
                return 1;
            }

            // 3，著者名称为三字者，依次取首字左上、右上两角和后两字的左上角。如：罗贯中=6075
            if (strResult.Length == 3)
            {
                strAuthorNumber = sjhms[0].Substring(0, 2)
                    + sjhms[1].Substring(0, 1)
                    + sjhms[2].Substring(0, 1);
                return 1;
            }

            // 4，著者名称为四字者，依次取各字的左上角。如：中田英寿=5645
            // 5，五字及以上字数者，均以前四字取号，方法同上。如：奥斯特洛夫斯基=2423
            if (strResult.Length >= 4)
            {
                strAuthorNumber = sjhms[0].Substring(0, 1)
                    + sjhms[1].Substring(0, 1)
                    + sjhms[2].Substring(0, 1)
                    + sjhms[3].Substring(0, 1);
                return 1;
            }

            strError = "error end";
            return -1;
        }
    }
}
